<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-06-15
 * Time: 11:24
 */
function tp_return($code = 0, $msg='ok')
{
    return ['code'=>$code,'msg'=>$msg];


}
/**
 * [阿里大鱼短信发送]
 */
function send_sms_aldy( $phone , $code='' ){
    if(!preg_match("/^1[34578]\d{9}$/", $phone)){

        return tp_return(1101 , '手机号码不合法');
    }
    $api_url = config('cfg_ALIDAYU_APIURL');
    $accessKeyId = config('cfg_ALIDAYU_ACCESSKEYID');
    $accessSecret = config('cfg_ALIDAYU_SECRET');
    $cutime = time();
    date_default_timezone_set('GMT');
    $df = date("Y-m-d",$cutime).'T'.date("H:i:s",$cutime).'Z';
    $param['SignatureMethod'] = "HMAC-SHA1";
    $param['SignatureNonce'] = createUUid();
    $param['AccessKeyId'] = $accessKeyId;
    $param['SignatureVersion'] = "1.0";
    $param['Timestamp'] = $df;
    $param['Format'] = "JSON";

    $param['Action'] = "SendSms";
    $param['Version'] = config('cfg_ALIDAYU_VERSION');
    $param['RegionId'] = "cn-hangzhou";
    $param['PhoneNumbers'] = $phone;
    $param['SignName'] = config('cfg_ALIDAYU_SIGN_NAME');
    if($code){
        //验证码模板
        $param['TemplateCode'] = config('cfg_ALIDAYU_TemplateCode');//C('alidayu.register_tpl.code');
        $param['TemplateParam'] = "{\"code\":\"$code\"}";//C('alidayu.register_tpl.param');
    }else{
        //短信推送模板
        $param['TemplateCode'] = config('cfg_ALIDAYU_TemplateCode');
    }

    ksort($param);
    $url = http_build_query($param);
    $ture_url = $url;
    $prefix = "GET&".urlencode("/")."&";
    $finalfix = speacialChar($url);
    $new_url = $prefix.$finalfix;
//    $signature = urlencode($hmac_sha1_str);
    $signature = base64_encode(hash_hmac('sha1', $new_url, $accessSecret.'&', true));
    $signature = speacialChar($signature);
    $url = $api_url.'?Signature='.$signature.'&'.$ture_url;
    $ret = curl_get($url);
    if($ret['Code']=='OK'){
        return tp_return( 0 , '发送成功' );
    }else{
        return tp_return( 1 , $ret['Message'] );
    }
}

/**
 * [createUUid description]
 * @return [type] [description]
 */
function createUUid()
{
    if (function_exists ( 'com_create_guid' )) {
        return com_create_guid ();
    } else {
        mt_srand ( ( double ) microtime () * 10000 );
        $charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) ); //根据当前时间（微秒计）生成唯一id.
        $hyphen = chr ( 45 ); // "-"
        $uuid = '' . //chr(123)// "{"
            substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 );
        //.chr(125);// "}"
        return $uuid;
    }
}

function speacialChar($str)
{
    $str = urlencode($str);
    return $str;
}

/**
 * [curl_get description]
 * @param  [type] $url [description]
 * @return [type]      [description]
 */
function curl_get($url){
    $curl = curl_init();
    curl_setopt ( $curl, CURLOPT_URL, $url );
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
    $output = curl_exec ( $curl );
    if(curl_errno($curl)){
        echo 'Curl error: ' . curl_error($curl);
        exit;
    }
    curl_close ( $curl );
    $ret = json_decode($output,true);
    return $ret;
}


function check_str($string, $isurl = false)
{
    $string =
        preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/','',$string);
    $string =
        str_replace(array("\0","%00","\r"),'',$string);
    empty($isurl) &&
    $string = preg_replace("/&(?!(#[0-9]+|[a-z]+);)/si",'&',$string);

    $string = str_replace(array("%3C",'<'),'<',$string);
    $string =
        str_replace(array("%3E",'>'),'>',$string);
    $string =
        str_replace(array('"',"'","\t",' '),array('“','‘',' ',' '),$string);
    return trim($string);
}

